package hammersmith.local.ui.gui;

import hammersmith.local.management.ShopManagement;
import hammersmith.local.valueobjects.Customer;
import hammersmith.local.valueobjects.Employee;
import hammersmith.local.valueobjects.Person;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JPasswordField;
import java.awt.Color;
import javax.swing.ImageIcon;

public class LoginMask extends JFrame {

	ShopManagement shopManager;
	private static final long serialVersionUID = -2720535732853922943L;
	private JTextField txtId;
	private JPasswordField txtPassword;

	public LoginMask() {

		shopManager = new ShopManagement("","");
		shopManager = shopManager.loadDB();								
		
		// CONTENT PANE
		getContentPane().setBackground(Color.WHITE);
		this.setSize(281, 260);
		this.setLocationRelativeTo(null);
		this.setTitle("ShoPPerz - Login");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setLayout(null);

		// MAIN PANEL
		JPanel pnlMain = new JPanel();
		pnlMain.setBackground(Color.WHITE);

		pnlMain.setBounds(6, 6, 269, 226);
		getContentPane().add(pnlMain);
		pnlMain.setLayout(null);

		// LABELS
		JLabel lblId = new JLabel("ID:");
		lblId.setHorizontalAlignment(SwingConstants.RIGHT);
		lblId.setBounds(65, 75, 24, 16);
		pnlMain.add(lblId);

		JLabel lblWelcomeTo = new JLabel("Welcome to ShoPPerz!");
		lblWelcomeTo.setFont(new Font("Calibri", Font.BOLD, 19));
		lblWelcomeTo.setHorizontalAlignment(SwingConstants.CENTER);
		lblWelcomeTo.setBounds(6, 6, 257, 16);
		pnlMain.add(lblWelcomeTo);

		JLabel lblPasswort = new JLabel("Passwort:");
		lblPasswort.setBounds(28, 103, 61, 16);
		pnlMain.add(lblPasswort);

		// TEXTFELDER
		txtId = new JTextField();
		txtId.setBounds(93, 69, 134, 28);
		pnlMain.add(txtId);
		txtId.setColumns(10);

		// BUTTONS
		JButton btnLogin = new JButton("Login");
		btnLogin.setIcon(new ImageIcon(LoginMask.class.getResource("/javax/swing/plaf/metal/icons/ocean/computer.gif")));
		btnLogin.setBounds(93, 127, 134, 29);
		pnlMain.add(btnLogin);

		// RADIOS
		JRadioButton radioEmployee = new JRadioButton("Mitarbeiter");
		radioEmployee.setBounds(43, 34, 100, 23);
		radioEmployee.setSelected(true);
		pnlMain.add(radioEmployee);

		final JRadioButton radioCustomer = new JRadioButton("Kunde");
		radioCustomer.setBounds(155, 34, 79, 23);
		pnlMain.add(radioCustomer);

		ButtonGroup radioGroup = new ButtonGroup();
		radioGroup.add(radioCustomer);
		radioGroup.add(radioEmployee);
		
		// TEXTFIELDS
		txtPassword = new JPasswordField();
		txtPassword.setBounds(93, 97, 134, 28);
		pnlMain.add(txtPassword);
		
		JButton btnRegister = new JButton("Registrieren");
		btnRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new RegisterMask(shopManager);
				shopManager.saveDB();
			}
		});
		btnRegister.setIcon(new ImageIcon(LoginMask.class.getResource("/javax/swing/plaf/metal/icons/ocean/iconify-pressed.gif")));
		btnRegister.setBounds(157, 185, 106, 35);
		pnlMain.add(btnRegister);
		
		JLabel lblNeuHier = new JLabel("Neu hier? ->");
		lblNeuHier.setBounds(69, 193, 88, 16);
		pnlMain.add(lblNeuHier);

		// Actions!
		btnLogin.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				Person found=null;
				String user = "";
				String pw = "";
				user = txtId.getText();
				
				char[] pwArray = txtPassword.getPassword();
				for(char c : pwArray){
					pw+=c;
				}
				
				if(radioCustomer.isSelected()){
					found = shopManager.loginCustomer(user, pw);
				} else {
					found = shopManager.loginEmployee(user, pw);
				}

				if (found == null) {
					JOptionPane
							.showMessageDialog(null, "Logindaten Inkorrekt!");
					txtId.setText("");
					txtPassword.setText("");
				} else {
					JOptionPane.showMessageDialog(
							null,
							"Login Erfolgreich! \n Hallo "
									+ found.getFirstname() + " "
									+ found.getLastname());

					dispose();
					if(found instanceof Customer){
						new CustomerMask(shopManager, (Customer) found);						
					} else {
						new EmployeeMask(shopManager, (Employee) found);
					}
				}
			}
		});

		this.setVisible(true);
	}

	public static void main(String[] args) {
		new LoginMask();
	}
}
